package com.ynsj.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ynsj.domain.SysPrivilege;
import com.ynsj.domain.SysRole;
import com.ynsj.model.R;
import com.ynsj.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;

@RestController
@RequestMapping("/roles")
@Api(tags = "角色管理")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 条件分页查询-》角色
     * @param sysRolePage
     * @param name
     * @return
     */
    @GetMapping
    @ApiOperation(value = "角色条件分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current",value = "当前页"),
            @ApiImplicitParam(name = "size",value = "每页显示的大小"),
            @ApiImplicitParam(name = "name",value = "角色名称")
    })
    @PreAuthorize("hasAuthority('sys_role_query')")
    public R<Page<SysRole>> findByPage(@ApiIgnore Page<SysRole> sysRolePage,String name){
        return R.ok(sysRoleService.findRolePage(sysRolePage,name));
    }

    /**
     * 新增角色
     * @param sysRole
     * @return
     */
    @PostMapping
    @ApiOperation("新增角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysRole",value = "sysRole数据对象")
    })
    @PreAuthorize("hasAuthority('sys_role_create')")
    public R add(@RequestBody @Validated SysRole sysRole){
        boolean save = sysRoleService.save(sysRole);
        if(save){
            return R.ok("新增成功");
        }
        return R.fail("新增失败");
    }

    /**
     * 修改角色
     * @param sysRole
     * @return
     */
    @PatchMapping
    @ApiOperation("修改角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysRole",value = "sysRole数据对象")
    })
    @PreAuthorize("hasAuthority('sys_role_update')")
    public R update(@RequestBody @Validated SysRole sysRole){
        boolean update = sysRoleService.updateById(sysRole);
        if(update){
            return R.ok("更新成功");
        }
        return R.fail("更新失败");
    }

    /**
     * 删除角色
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    @ApiOperation("删除角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids",value = "要删除的角色id数组")
    })
    @PreAuthorize("hasAuthority('sys_role_delete')")
    public R delete(@RequestBody String[] ids){
        if (ids==null || ids.length == 0){
            return R.fail("请选择至少一条数据删除");
        }
        boolean remove = sysRoleService.removeByIds(Arrays.asList(ids));
        if(remove){
            return R.ok("删除成功");
        }
        return R.fail("删除失败");
    }
}
